package com.dcivision.framework;

import java.sql.Connection;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.MessageResources;

import com.dcivision.framework.dao.GlobalReferenceDAObject;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
import com.dcivision.workflow.web.ListWorkflowProgressForm;
import com.dcivision.workflow.web.ListWorkflowRecordForm;

public class DcivisionManager {
  private static final Log log = LogFactory.getLog(DcivisionManager.class);

  public void initSystemDAO(Connection connection) throws Exception {
    GlobalReferenceDAObject gloRefDAO = new GlobalReferenceDAObject(connection);

    // Initialize system parameters hash table.
    Hashtable sysParaHash = gloRefDAO.getAllSysParameter();
    // Override the database system parameters configuration with property
    // file
    sysParaHash.putAll(Utility.parseResourceBundle(GlobalConstant.CONFIG_FILENAME));
    SystemParameterFactory.init(sysParaHash);

    Hashtable userInfoHash = gloRefDAO.getAllUserInfo();
    UserInfoFactory.init(userInfoHash);
    DcivisionDataSource ds = new DcivisionDataSource(DatabaseManager.getCurrentConnection());
    DataSourceFactory.init(ds, new DcivisionLog());

  }

  public static SessionContainer getSessionContainer(Connection connection, UserRecord userRecord) {
    SessionContainer sessionCtn = new SessionContainer();
    UserGroupDAObject userGroupDAO = new UserGroupDAObject(sessionCtn, connection);
    try {
      // Get User Groups Information.
      List userGroups = userGroupDAO.getListByUserRecordIDGroupType(userRecord.getID(), UserGroup.GROUP_TYPE_PUBLIC);
      sessionCtn.getPermissionManager().setUserGroups(userGroups);

      // Get User Roles and Group Role Information (Union them with no
      // duplicate roles).
      UserRoleDAObject userRoleDAO = new UserRoleDAObject(sessionCtn, connection);
      List userRoles = userRoleDAO.getListByUserRecordID(userRecord.getID());

      for (int i = 0; i < userGroups.size(); i++) {
        UserGroup tmpUserGroup = (UserGroup) userGroups.get(i);
        List userRolesByGroup = userRoleDAO.getListByUserGroupID(tmpUserGroup.getID());
        for (int j = 0; j < userRolesByGroup.size(); j++) {
          if (!userRoles.contains(userRolesByGroup.get(j))) {
            userRoles.add(userRolesByGroup.get(j));
          }
        }
      }
      sessionCtn.getPermissionManager().setUserRoles(userRoles);
      sessionCtn.setUserRecord(userRecord);
    } catch (ApplicationException e) {
      log.error(e, e);
    }
    return sessionCtn;
  }

  public ListWorkflowProgressForm getListWorkflowProgressForm() {
    ListWorkflowProgressForm searchForm = new ListWorkflowProgressForm();
    searchForm.setCurStartRowNo("1");
    searchForm.setPage(0);
    searchForm.setPageOffset("100");
    searchForm.setShowFullList("N");
    searchForm.setSortAttribute("UPDATE_DATE");
    searchForm.setSortOrder("DESC");

    return searchForm;
  }

  public ListWorkflowRecordForm getListWorkflowRecordForm() {
    ListWorkflowRecordForm searchForm = new ListWorkflowRecordForm();
    searchForm.setCurStartRowNo("1");
    searchForm.setPage(0);
    searchForm.setPageOffset("100");
    searchForm.setSortAttribute("WORKFLOW_CODE");
    searchForm.setSortOrder("DESC");

    return searchForm;
  }

  public UserRecord getUserRecord() {
    UserRecord userRecord = new UserRecord();
    userRecord.setID(Integer.valueOf("1"));
    userRecord.setFullName("samlin.zhang");
    userRecord.setUserLinkID(Integer.valueOf("1"));
    return userRecord;
  }

  public void initMessageFactory() {
    DcivisionMessageResourcesFactory refactory = new DcivisionMessageResourcesFactory();
    MessageResources tt = new DcivisionMessageResources(refactory, "config");// new
    // MessageResources();
    MessageResourcesFactory.init(tt, tt);

  }

}
